added one spec for twitter user agent

Albert Sun 11 年之前
父节点
当前提交
a6167d5803
共有 3 个文件被更改,包括 29 次插入22 次删除
  1. 0 22
      app/models/agents/twitter_user_agent.rb
  2. 1 0
      spec/data_fixtures/user_tweets.json
  3. 28 0
      spec/models/agents/twitter_user_agent_spec.rb

+ 0 - 22
app/models/agents/twitter_user_agent.rb

@@ -129,19 +129,6 @@ module Agents
129 129
       }
130 130
     end
131 131
 
132
-    # def process_tweet(filter, status)
133
-    #   if options[:generate] == "counts"
134
-    #     # Avoid memory pollution
135
-    #     me = Agent.find(id)
136
-    #     me.memory[:filter_counts] ||= {}
137
-    #     me.memory[:filter_counts][filter.to_sym] ||= 0
138
-    #     me.memory[:filter_counts][filter.to_sym] += 1
139
-    #     me.save!
140
-    #   else
141
-    #     create_event :payload => status.merge(:filter => filter.to_s)
142
-    #   end
143
-    # end
144
-
145 132
     def check
146 133
       Twitter.configure do |config|
147 134
         config.consumer_key = options[:consumer_key]
@@ -151,10 +138,8 @@ module Agents
151 138
       end
152 139
 
153 140
       since_id = memory[:since_id] || nil
154
-      max_id = memory[:max_id] || nil
155 141
       opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true}
156 142
       opts.merge! :since_id => since_id unless since_id.nil?
157
-      opts.merge! :max_id => max_id unless max_id.nil?
158 143
 
159 144
       tweets = Twitter.user_timeline(options[:username], opts)
160 145
 
@@ -165,13 +150,6 @@ module Agents
165 150
       end
166 151
 
167 152
       save!
168
-      # if memory[:filter_counts] && memory[:filter_counts].length > 0
169
-      #   memory[:filter_counts].each do |filter, count|
170
-      #     create_event :payload => { :filter => filter.to_s, :count => count, :time => Time.now.to_i }
171
-      #   end
172
-      #   memory[:filter_counts] = {}
173
-      #   save!
174
-      # end
175 153
     end
176 154
   end
177 155
 end

+ 1 - 0
spec/data_fixtures/user_tweets.json

@@ -0,0 +1 @@
1
+[{"created_at":"Sun Apr 07 20:34:17 +0000 2013","id":320997982096990208,"id_str":"320997982096990208","text":"@christophe971 Cool project!","source":"web","truncated":false,"in_reply_to_status_id":320990180205002752,"in_reply_to_status_id_str":"320990180205002752","in_reply_to_user_id":12574212,"in_reply_to_user_id_str":"12574212","in_reply_to_screen_name":"christophe971","user":{"id":9813372,"id_str":"9813372","name":"Andrew Cantino","screen_name":"tectonic","location":"San Francisco, CA","description":"Experimentalist, web developer, and VP of Engineering at @Mavenlink.","url":"http://andrewcantino.com","entities":{"url":{"urls":[{"url":"http://andrewcantino.com","expanded_url":null,"indices":[0,24]}]},"description":{"urls":[]}},"protected":false,"followers_count":999,"friends_count":492,"listed_count":39,"created_at":"Wed Oct 31 03:16:39 +0000 2007","favourites_count":132,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":3433,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http://a0.twimg.com/images/themes/theme5/bg.gif","profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme5/bg.gif","profile_background_tile":false,"profile_image_url":"http://a0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"christophe971","name":"Christophe Maximin","id":12574212,"id_str":"12574212","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"en"},{"created_at":"Sun Apr 07 20:34:06 +0000 2013","id":320997936773349378,"id_str":"320997936773349378","text":"@christophe971 Fair enough.  I've previously worked on something very similar and wondered if you're looking for collaborators.","source":"web","truncated":false,"in_reply_to_status_id":320990180205002752,"in_reply_to_status_id_str":"320990180205002752","in_reply_to_user_id":12574212,"in_reply_to_user_id_str":"12574212","in_reply_to_screen_name":"christophe971","user":{"id":9813372,"id_str":"9813372","name":"Andrew Cantino","screen_name":"tectonic","location":"San Francisco, CA","description":"Experimentalist, web developer, and VP of Engineering at @Mavenlink.","url":"http://andrewcantino.com","entities":{"url":{"urls":[{"url":"http://andrewcantino.com","expanded_url":null,"indices":[0,24]}]},"description":{"urls":[]}},"protected":false,"followers_count":999,"friends_count":492,"listed_count":39,"created_at":"Wed Oct 31 03:16:39 +0000 2007","favourites_count":132,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":3433,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http://a0.twimg.com/images/themes/theme5/bg.gif","profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme5/bg.gif","profile_background_tile":false,"profile_image_url":"http://a0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"christophe971","name":"Christophe Maximin","id":12574212,"id_str":"12574212","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"en"},{"created_at":"Sun Apr 07 19:59:21 +0000 2013","id":320989192488382464,"id_str":"320989192488382464","text":"@christophe971 Is denisthebot open source?","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":12574212,"in_reply_to_user_id_str":"12574212","in_reply_to_screen_name":"christophe971","user":{"id":9813372,"id_str":"9813372","name":"Andrew Cantino","screen_name":"tectonic","location":"San Francisco, CA","description":"Experimentalist, web developer, and VP of Engineering at @Mavenlink.","url":"http://andrewcantino.com","entities":{"url":{"urls":[{"url":"http://andrewcantino.com","expanded_url":null,"indices":[0,24]}]},"description":{"urls":[]}},"protected":false,"followers_count":999,"friends_count":492,"listed_count":39,"created_at":"Wed Oct 31 03:16:39 +0000 2007","favourites_count":132,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":3433,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http://a0.twimg.com/images/themes/theme5/bg.gif","profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme5/bg.gif","profile_background_tile":false,"profile_image_url":"http://a0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"christophe971","name":"Christophe Maximin","id":12574212,"id_str":"12574212","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"nl"},{"created_at":"Sun Apr 07 00:18:42 +0000 2013","id":320692069385527296,"id_str":"320692069385527296","text":"Netflix won’t allow a password longer than 10 characters.  Fail.","source":"<a href=\"http://tapbots.com/software/tweetbot/mac\" rel=\"nofollow\">Tweetbot for Mac</a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":9813372,"id_str":"9813372","name":"Andrew Cantino","screen_name":"tectonic","location":"San Francisco, CA","description":"Experimentalist, web developer, and VP of Engineering at @Mavenlink.","url":"http://andrewcantino.com","entities":{"url":{"urls":[{"url":"http://andrewcantino.com","expanded_url":null,"indices":[0,24]}]},"description":{"urls":[]}},"protected":false,"followers_count":999,"friends_count":492,"listed_count":39,"created_at":"Wed Oct 31 03:16:39 +0000 2007","favourites_count":132,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":3433,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http://a0.twimg.com/images/themes/theme5/bg.gif","profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme5/bg.gif","profile_background_tile":false,"profile_image_url":"http://a0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":{"id":"8d2591b0d50daf32","url":"https://api.twitter.com/1.1/geo/id/8d2591b0d50daf32.json","place_type":"neighborhood","name":"Lower Haight","full_name":"Lower Haight, San Francisco","country_code":"US","country":"United States","polylines":[],"bounding_box":{"type":"Polygon","coordinates":[[[-122.437382,37.769551],[-122.421482,37.769551],[-122.421482,37.77602],[-122.437382,37.77602]]]},"attributes":{}},"contributors":null,"retweet_count":2,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"en"},{"created_at":"Sat Apr 06 18:42:44 +0000 2013","id":320607521532432384,"id_str":"320607521532432384","text":"@ryanmaxwell @draconisnz let me know if you use it, or have suggestions!","source":"<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>","truncated":false,"in_reply_to_status_id":320454615885807616,"in_reply_to_status_id_str":"320454615885807616","in_reply_to_user_id":14658141,"in_reply_to_user_id_str":"14658141","in_reply_to_screen_name":"ryanmaxwell","user":{"id":9813372,"id_str":"9813372","name":"Andrew Cantino","screen_name":"tectonic","location":"San Francisco, CA","description":"Experimentalist, web developer, and VP of Engineering at @Mavenlink.","url":"http://andrewcantino.com","entities":{"url":{"urls":[{"url":"http://andrewcantino.com","expanded_url":null,"indices":[0,24]}]},"description":{"urls":[]}},"protected":false,"followers_count":999,"friends_count":492,"listed_count":39,"created_at":"Wed Oct 31 03:16:39 +0000 2007","favourites_count":132,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":3433,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http://a0.twimg.com/images/themes/theme5/bg.gif","profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme5/bg.gif","profile_background_tile":false,"profile_image_url":"http://a0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"ryanmaxwell","name":"Ryan Maxwell","id":14658141,"id_str":"14658141","indices":[0,12]},{"screen_name":"draconisNZ","name":"Dan","id":14979092,"id_str":"14979092","indices":[13,24]}]},"favorited":false,"retweeted":false,"lang":"en"}]

+ 28 - 0
spec/models/agents/twitter_user_agent_spec.rb

@@ -0,0 +1,28 @@
1
+require 'spec_helper'
2
+
3
+describe Agents::TwitterUserAgent do
4
+  before do
5
+    # intercept the twitter API request for @tectonic's user profile
6
+    stub_request(:any, /tectonic/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200)
7
+  
8
+    @opts = {
9
+      :username => "tectonic",
10
+      :expected_update_period_in_days => "2",
11
+      :consumer_key => "---",
12
+      :consumer_secret => "---",
13
+      :oauth_token => "---",
14
+      :oauth_token_secret => "---"
15
+    }
16
+
17
+    @checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => @opts)
18
+    @checker.user = users(:bob)
19
+    @checker.save!
20
+  end
21
+
22
+  describe "#check" do
23
+    it "should check for changes" do
24
+      lambda { @checker.check }.should change { Event.count }.by(5)
25
+    end
26
+  end
27
+
28
+end